﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Customer
    {

        public static void insert(CustomerEntity customer)
        {
            DbHelper.ExecuteNonQuery("p_Customer_insert",
              customer.Username,
              customer.Email,
              customer.Password,
              customer.Realname,
              customer.Phone,             
              customer.CompanyId,             
              customer.IsAdmin,
              customer.IsRefuse,
              customer.expireDt,
              customer.Position
               );
        }

        public static void update(CustomerEntity customer)
        {
            DbHelper.ExecuteNonQuery("p_Customer_update",
              customer.CustomerId,
              customer.Username,
              customer.Email,
              customer.Password,
              customer.Realname,
              customer.Phone,
              customer.CompanyId,
              customer.IsAdmin,
              customer.IsRefuse,
              customer.expireDt,
              customer.Position
               );
        }


        public static void delete(int customerId)
        {
            DbHelper.ExecuteNonQuery("p_Customer_delete",
                customerId);
        }

        public static CustomerEntity getById(int customerId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_Customer_getById", customerId);
            CustomerEntity customer = new CustomerEntity();
            if (dr != null)
            {
                customer.CustomerId = customerId;
                customer.Username = dr["username"].ToString();
                customer.Email = dr["email"].ToString();
                customer.Password = dr["password"].ToString();
                customer.Realname = dr["realname"].ToString();
                customer.Phone = dr["phone"].ToString();
                customer.RegisterDt = Convert.ToDateTime(dr["registerDt"]);
                customer.CompanyId = Convert.ToInt32(dr["companyId"]);
                customer.Permissions = dr["permissions"].ToString();
                customer.IsAdmin = Convert.ToBoolean(dr["isAdmin"]);
                customer.IsRefuse = Convert.ToBoolean(dr["isRefuse"]);
                if (dr["lastLoginDate"] != DBNull.Value)
                    customer.LastLoginDate = Convert.ToDateTime(dr["lastLoginDate"]);
                if (dr["currentLoginDate"] != DBNull.Value)
                    customer.CurrentLoginDate = Convert.ToDateTime(dr["currentLoginDate"]);
                if (dr["expireDt"] != DBNull.Value)
                    customer.expireDt = Convert.ToDateTime(dr["expireDt"]);
                customer.Position = dr["Position"].ToString();
            }
            return customer;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>StatusCode描述：8-用户名不存在;9-密码不正确;10-账号已过期；11-账号已禁用；12-验证通过</returns>
        public static CustomerEntity login(string username, string password)
        {
            CustomerEntity customer = new CustomerEntity();
            DataRow dr = DbHelper.ExecuteDataRow("[dbo].[p_Customer_login]", username, password);
            if (dr != null)
            {
                if(dr["customerId"]!=DBNull.Value)
                    customer.CustomerId = Convert.ToInt32(dr["customerId"]);
                if(dr["companyId"]!=DBNull.Value)
                    customer.CompanyId = Convert.ToInt32(dr["companyId"]);
                if (dr["companyType"] != DBNull.Value)
                    customer.CompanyType = dr["companyType"].ToString();
                if(dr["StatusCode"]!=DBNull.Value)
                    customer.StatusCode = Convert.ToInt32(dr["StatusCode"]);
            }

            return customer;
        }

        public static void updatePwd(CustomerEntity cus)
        {
            DbHelper.ExecuteNonQuery("p_Customer_updatePwd", cus.CustomerId, cus.Password, cus.IP, cus.Oldpassword);
        }
        public static bool isExistUser(string username)
        {
            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_Customer_isExistUser", username)) == 1)
                return true;
            else
                return false;
        }

        public static DataRow getByUsername(string username)
        {
            return DbHelper.ExecuteDataRow("p_Customer_getByUsername",username);
        }

        public static bool allowAccess(int customerid, string powers)
        {
            return Convert.ToBoolean(DbHelper.ExecuteScalar("p_Customer_allowAccess", customerid, powers));
        }

       
        public static void updatePermission(int cusId, string permission)
        {
            DbHelper.ExecuteNonQuery("p_Customer_updatePermission", cusId, permission);
        }

        public static void updatePwdByadmin(int customerid, string password,int adminid,string loginIp)
        {
            DbHelper.ExecuteNonQuery("p_Customer_updatePwdByAdmin", customerid, password,adminid,loginIp);
        }
        
    }
}
